import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {PIID} from "./PIID";
import {FactoryVersion} from "./FactoryVersion";
import {Unsigned16} from "./Unsigned16";
import {ProtocolConformance} from "./ProtocolConformance";
import {FunctionConformance} from "./FunctionConformance";
import {Unsigned8} from "./Unsigned8";
import {Unsigned32} from "./Unsigned32";
import {ConnectResponseInfo} from "./ConnectResponseInfo";


export class Connect_Response implements AxdrType{

    dataCode : Buffer | null = null;
    piid_acd : PIID | null = null;
    fver : FactoryVersion | null = null;
    wver : Unsigned16 | null = null;
    wpro : ProtocolConformance | null = null;
    wfun : FunctionConformance | null = null;
    wSendSize : Unsigned16 | null = null;
    wRecvSize : Unsigned16 | null = null;
    wRecvWnd : Unsigned8 | null = null;
    wMaxApdu : Unsigned16 | null = null;
    wMaxIdle : Unsigned32 | null = null;
    wConnObj : ConnectResponseInfo | null = null;

    setDataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(piid_acd : PIID, fver : FactoryVersion, wver : Unsigned16, wpro : ProtocolConformance,
            wfun : FunctionConformance, wSendSize : Unsigned16, wRecvSize : Unsigned16,
            wRecvWnd : Unsigned8, wMaxApdu : Unsigned16, wMaxIdle : Unsigned32,
            wConnObj : ConnectResponseInfo){
        this.piid_acd = piid_acd;
        this.fver = fver;
        this.wver = wver;
        this.wpro = wpro;
        this.wfun = wfun;
        this.wSendSize = wSendSize;
        this.wRecvSize = wRecvSize;
        this.wRecvWnd = wRecvWnd;
        this.wMaxApdu = wMaxApdu;
        this.wMaxIdle = wMaxIdle;
        this.wConnObj = wConnObj;
    }

    setPIID_ACD(piid_acd : PIID){
        this.piid_acd = piid_acd;
    }

    setFver(fver : FactoryVersion){
        this.fver = fver;
    }

    setWver(wver : Unsigned16){
        this.wver = wver;
    }

    setWpro(wpro : ProtocolConformance){
        this.wpro = wpro;
    }

    setWfun(wfun : FunctionConformance){
        this.wfun = wfun;
    }

    setWSendSize(wSendSize : Unsigned16){
        this.wSendSize = wSendSize;
    }

    setWRecvSize(wRecvSize : Unsigned16){
        this.wRecvSize = wRecvSize;
    }

    setWRecvWnd(wRecvWnd : Unsigned8){
        this.wRecvWnd = wRecvWnd;
    }

    setWMaxApdu(wMaxApdu : Unsigned16){
        this.wMaxApdu = wMaxApdu;
    }

    setWMaxIdle(wMaxIdle : Unsigned32){
        this.wMaxIdle = wMaxIdle;
    }

    setWConnObj(wConnObj : ConnectResponseInfo){
        this.wConnObj = wConnObj;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd .decode(input);

        this.fver = new FactoryVersion();
        codeLength += this.fver.decode(input);

        this.wver = new Unsigned16();
        this.wver.set_const();
        codeLength += this.wver.decode(input);

        this.wpro = new ProtocolConformance();
        codeLength += this.wpro.decode(input);

        this.wfun = new FunctionConformance();
        codeLength += this.wfun.decode(input);

        this.wSendSize = new Unsigned16();
        this.wSendSize.set_const();
        codeLength += this.wSendSize.decode(input);

        this.wRecvSize = new Unsigned16();
        this.wRecvSize.set_const();
        codeLength += this.wRecvSize.decode(input);

        this.wRecvWnd = new Unsigned8();
        this.wRecvWnd.set_const();
        codeLength += this.wRecvWnd.decode(input);

        this.wMaxApdu = new Unsigned16();
        this.wMaxApdu.set_const();
        codeLength += this.wMaxApdu.decode(input);

        this.wMaxIdle = new Unsigned32();
        this.wMaxIdle.set_const();
        codeLength += this.wMaxIdle.decode(input);

        this.wConnObj = new ConnectResponseInfo();
        codeLength += this.wConnObj.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.wConnObj != null && this.wMaxIdle != null
                && this.wMaxApdu != null && this.wRecvWnd != null
                && this.wRecvSize != null && this.wSendSize != null
                && this.wfun != null && this.wpro != null && this.wver != null
                && this.piid_acd != null && this.fver != null){
                codeLength = 0;
                codeLength += this.wConnObj.encode(output);
                codeLength += this.wMaxIdle.encode(output);
                codeLength += this.wMaxApdu.encode(output);
                codeLength += this.wRecvWnd.encode(output);
                codeLength += this.wRecvSize.encode(output);
                codeLength += this.wSendSize.encode(output);
                codeLength += this.wfun.encode(output);
                codeLength += this.wpro.encode(output);
                codeLength += this.wver.encode(output);
                codeLength += this.fver.encode(output);
                codeLength += this.piid_acd.encode(output);
            }else{
                throw new Error("wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || || fver || piid_acd is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.wConnObj != null && this.wMaxIdle != null
            && this.wMaxApdu != null && this.wRecvWnd != null
            && this.wRecvSize != null && this.wSendSize != null
            && this.wfun != null && this.wpro != null && this.wver != null
            && this.piid_acd != null && this.fver != null) {
            return "sequence: {"+ "piid_acd: " + this.piid_acd + ", fver: " + this.fver + ", wver: " + this.wver + ", wpro: " + this.wpro + ", wfun: " + this.wfun + ", wsendsize: " + this.wSendSize + ", wrecvsize: " + this.wRecvSize + ", wrecvwnd: " + this.wRecvWnd + ", wmaxapdu: " + this.wMaxApdu + ", wmaxidle: " + this.wMaxIdle + ", wconnobj: " + this.wConnObj  + "}";
        } else {
            return "wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || || fver || piid_acd is null";
        }
    }

}
